#include "htmltable.h"
#include <QObject>
#include <QFileDialog>
#include <QIODevice>

HtmlTable::HtmlTable()
{
    sCaption = QObject::tr("统计报表");
    nWidth = 300;
    fBorder = 0.5f;
    nCellPadding = 50;
    eAlign = CENTER;

    sTable = "";
    sTh = "";
    sTr = "";
}

HtmlTable::~HtmlTable()
{

}

QString HtmlTable::ToHtmlTableString()
{
    QString table = "";

    table = QObject::tr("<table border=\"%1\" width=\"%2\" cellpadding=\"%3\">").
            arg(fBorder).arg(nWidth).arg(nCellPadding) + sTable +  "</table>";

    return table;
}

void HtmlTable::Clear()
{
    sTable.clear();
    sTh.clear();
    sTr.clear();
}

void HtmlTable::BeginTh()
{
    sTh.clear();
    sTh = "<tr>";
}

void HtmlTable::EndTh()
{
    sTh = sTh + "</tr>";
    sTable = sTh + sTable;
}

void HtmlTable::AddTh(QString th)
{
    QString align = "";
    if ( eAlign == LEFT )
    {
        align = QObject::tr("left");
    }
    else if ( eAlign == CENTER )
    {
       align = QObject::tr("center");
    }
    else if ( eAlign == RIGHT )
    {
       align = QObject::tr("right");
    }
    else
    {
        ;
    }
    if ( th.isEmpty() )
    {
        th = QObject::tr("&nbsp;");
    }

    QString data = QObject::tr("<th align=\"%1\">").arg(align);
    data += th + QObject::tr("</th>");

    sTh += data;
}

void HtmlTable::BeginTr()
{
    sTr.clear();
    sTr = QObject::tr("<tr>");
}

void HtmlTable::EndTr()
{
    sTr += QObject::tr("</tr>");
    sTable += sTr;
}

void HtmlTable::AddTd(QVariant td)
{
    QString align = "";
    if ( eAlign == LEFT )
    {
        align = QObject::tr("left");
    }
    else if ( eAlign == CENTER )
    {
       align = QObject::tr("center");
    }
    else if ( eAlign == RIGHT )
    {
       align = QObject::tr("right");
    }
    else
    {
        ;
    }

    QString tabledata = td.toString();
    if ( tabledata.isEmpty() )
    {
        tabledata = QObject::tr("&nbsp;");
    }

    QString data = QObject::tr("<td align=\"%1\">").arg(align);
    data = data + tabledata + QObject::tr("</td>");

    sTr += data;
}

void HtmlTable::SetCaption(QString caption)
{
    sCaption = caption;
}

void HtmlTable::SetWidth(int width)
{
    nWidth = width;
}

void HtmlTable::SetBorder(float border)
{
    fBorder = border;
}

void HtmlTable::SetCellPadding(unsigned int cp)
{
    nCellPadding = cp;
}


void HtmlTable::SetAlign(Align align)
{
    eAlign = align;
}
